package com.demo.voice2openapp.activity;

import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Gravity;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.github.ybq.android.spinkit.SpriteFactory;
import com.github.ybq.android.spinkit.Style;
import com.demo.voice2openapp.present.MainPresent;
import com.demo.voice2openapp.present.MainPresentImpl;

import java.util.List;
import java.util.Locale;

public class MainActivity extends Activity implements MainView {

    public static final String TAG = "voice2app";
    private MainPresent mainPresent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        int halfTransparentColor = Color.parseColor("#99000000");

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//因为不是所有的系统都可以设置颜色的，在4.4以下就不可以。。有的说4.1，所以在设置的时候要检查一下系统版本是否是4.1以上
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(halfTransparentColor);
        }

        FrameLayout container = new FrameLayout(this);
        ViewGroup.LayoutParams pParams = new ViewGroup.LayoutParams(200,200);
        container.setBackgroundColor(halfTransparentColor);
        container.setLayoutParams(pParams);

        ProgressBar progressBar = new ProgressBar(this);

        progressBar.setIndeterminateDrawable(SpriteFactory.create(Style.WAVE));

        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(50, 50);
        layoutParams.gravity = Gravity.CENTER;
        progressBar.setLayoutParams(layoutParams);


        container.addView(progressBar);
        setContentView(container);


        initView();
    }


    @TargetApi(Build.VERSION_CODES.M)
    public void initView() {

        Log.i(TAG, "initView");

        if (checkSelfPermission(Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(  new String[]{Manifest.permission.RECORD_AUDIO}, 1);
        } else {
            start();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode,   String[] permissions,  int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        for (int i = 0; i < permissions.length; i++) {
            if (grantResults[i] == PackageManager.PERMISSION_GRANTED && permissions[i] == Manifest.permission.RECORD_AUDIO) {
                start();
                return;
            } else {
                Log.i(TAG, "no permission : audio");
                finish();
            }
        }
    }

    private void start() {
        mainPresent = new MainPresentImpl(this, this);
        Handler handler = new Handler();
        mainPresent.startVoiceDictation();
        Log.i(TAG, "startVoiceDictation");
//        handler.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                Log.i(TAG, "stopVoiceDictation");
//                recoderSignPresent.stopVoiceDictation();
//                finish();
//            }
//        }, 3000);
    }

    @Override
    public void showToast(String str) {
        Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
    }


    @Override
    public void dicationFinished(String text) {
        Log.i("RecordSignBizImpl1", text);



        text = text.toLowerCase(Locale.ROOT);
        //查找本地安装列表
        List<PackageInfo> installedPackages = getApplication().getPackageManager().getInstalledPackages(0);
        for (PackageInfo packageInfo : installedPackages) {
            if (text.equals(packageInfo.applicationInfo.loadLabel(getPackageManager()).toString().toLowerCase(Locale.ROOT))) {
                Intent resolveIntent = getPackageManager().getLaunchIntentForPackage(packageInfo.packageName);
                startActivity(resolveIntent);
                break;
            }
        }
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mainPresent.onDestroy();
    }

    @Override
    protected void onResume() {
        mainPresent.onResume();
        super.onResume();
    }

    @Override
    protected void onPause() {
        mainPresent.onPause();
        super.onPause();
    }
}
